package com.skplanet.oneplusonewindow.moimserver.controller.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skplanet.oneplusonewindow.moimserver.domain.Moim_Member;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_Moim;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_User;
import com.skplanet.oneplusonewindow.moimserver.service.MoimService;
import com.skplanet.oneplusonewindow.moimserver.service.UserService;


@Controller
public class MoimJSONController {
	
	@Autowired
	private MoimService moimService;
	@Autowired
	private UserService userService;

	@ResponseBody	
	@RequestMapping( value = { "/json/moim/get", "/json/moim/get.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> list(
			@RequestParam(value="id", required=true)long id) {	
		
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_Moim moim = moimService.getMoim(id);
		
		mapJSON.put("moim", moim);
		
		return mapJSON;
	} 
		
	@ResponseBody
	@RequestMapping( value = { "/json/moim/addMoim", "/json/moim/addMoim.json" }, method = RequestMethod.GET, headers = { "Accept=application/json"} )
	public Map<String, Object> addMoim(
			@RequestParam(value="userName", required=true)String userName,
			@RequestParam(value="title", required=true)String title,
			@RequestParam(value="content", required=true)String content,
			@RequestParam(value="cover", defaultValue="1", required=false)int cover) {	
		
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		if(moimService.titleExists(title) == true) {
			
			mapJSON.put( "result", "titleExists" );
		}
		else {
			Moim_Moim moim = new Moim_Moim();
			moim.setTitle(title);
			moim.setContent(content);
			moim.setCoverNum(cover);
			moimService.addMoim(moim);
			
			Moim_User user = moimService.addMoim(userName, moim.getId());
			
			Moim_Member member = new Moim_Member();
			member.setPosition("king");
			member.setUserId(user.getId());
			moimService.addMember(moim.getId(), member);

			if(user.getMoim(moim.getId()) == moimService.getMoim(moim.getId()))
				mapJSON.put("result", "success");
			else 
				mapJSON.put("result", "fail");
			mapJSON.put("moim_id", moim.getId());
		}
		
		return mapJSON;
	}

	@ResponseBody	
	@RequestMapping( value = { "/json/moim/userList", "/json/moim/userList.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> userList(
			@RequestParam(value="id", required=true)long id) {	
		
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_Moim moim = moimService.getMoim(id);
		List<Moim_Member> members = moim.getMembers();
		mapJSON.put("member", members);
		
		return mapJSON;
	} 

	@ResponseBody	
	@RequestMapping( value = { "/json/moim/usernameList", "/json/moim/usernameList.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> userList2(
			@RequestParam(value="id", required=true)long id) {	
		
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		Moim_Moim moim = moimService.getMoim(id);
		List<Moim_Member> members = moim.getMembers();
		List<HashMap<String, Object>> users = new ArrayList<HashMap<String, Object>>();
		
		for(Moim_Member m : members) {
			HashMap<String, Object> tmp = new HashMap< String, Object >();
			Moim_User user =  userService.getUserFromId(m.getUserId());
			tmp.put("nickname", user.getNickname());
			tmp.put("name", user.getName());
			tmp.put("id", user.getId());
			users.add(tmp);
		}
		
		mapJSON.put("user", users);
		
		return mapJSON;
	} 
	
}
